package cli

import (
	"fmt"
	"go_cloud_station/store"
	"go_cloud_station/store/aliyun"
	"go_cloud_station/store/aws"
	"go_cloud_station/store/tx"

	"github.com/schollz/progressbar/v3"
	"github.com/spf13/cobra"
)

var (
	ossProvider string
	file        string
	objectKey   string
	bucketName  string
	bar         *progressbar.ProgressBar
)

var UploadCmd = &cobra.Command{
	Use:     "upload",
	Long:    "上传文件",
	Short:   "上传文件",
	Example: "upload -f file.txt",
	RunE: func(cmd *cobra.Command, args []string) (err error) {
		var uploader store.Uploader
		switch ossProvider {
		case "aliyun":
			uploader = aliyun.NewAliyunOssStore("cn-shenzhen", "LTAI5tEadeH3rAupuqF2E9o5", "rjuLILgGmmkmRtgZxxBfX8eoKWPpHw")
		case "tx":
			uploader = tx.NewTXStore()
		case "aws":
			uploader = aws.NewAWSStore()
		default:
			return fmt.Errorf("不支持的服务商：%s", ossProvider)
		}

		return uploader.Upload(&store.UploadOptions{
			BucketName: bucketName, ObjectKey: objectKey, FileName: file,
			ProcessFn: func(increment, transferred, total int64) {
				if bar == nil {
					bar = progressbar.NewOptions64(total,
						progressbar.OptionEnableColorCodes(true),
						progressbar.OptionShowBytes(true),
						progressbar.OptionSetWidth(15),
						progressbar.OptionSetDescription("上传文件..."),
						progressbar.OptionSetTheme(progressbar.Theme{
							Saucer:        "[white]=[reset]",
							SaucerHead:    "[white]>[reset]",
							SaucerPadding: " ",
							BarStart:      "[",
							BarEnd:        "]",
						}))
				}
				bar.Add64(increment)
			},
		})
	},
}

func init() {
	flags := UploadCmd.PersistentFlags()
	flags.StringVarP(&ossProvider, "provider", "p", "aliyun", "oss服务通商")
	flags.StringVarP(&file, "file", "f", "", "文件地址")
	flags.StringVarP(&objectKey, "objectKey", "o", "", "文件名")
	flags.StringVarP(&bucketName, "bucketName", "b", "", "存储桶")
	rootCmd.AddCommand(UploadCmd)
}
